<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类id" prop="catid">
        <el-input v-model="queryParams.catid" placeholder="请输入分类id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商家名字" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入商家名字" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商家评分" prop="score">
        <el-input v-model="queryParams.score" placeholder="请输入商家评分" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商家手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入商家手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商家地址" prop="address">
        <el-input v-model="queryParams.address" placeholder="请输入商家地址" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="发布状态" prop="publishstate">
        <el-input v-model="queryParams.publishstate" placeholder="请输入发布状态" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:merchant:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:merchant:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:merchant:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:merchant:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="merchantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商家id" align="center" prop="merchantid" />
      <el-table-column label="分类id" align="center" prop="catid" />
      <el-table-column label="商家名字" align="center" prop="name" />
      <el-table-column label="商家评分" align="center" prop="score" />
      <el-table-column label="商家手机号" align="center" prop="phone" />
      <el-table-column label="商家地址" align="center" prop="address" />
      <el-table-column label="起送价格" align="center" prop="deliveryprice" />
      <el-table-column label="发布状态" align="center" prop="publishstate" >
 
        <template slot-scope="scope"> 
          <el-tag v-if="scope.row.publishstate=='0'" type="danger">未发布</el-tag>
          <el-tag v-else>已发布</el-tag>
        </template>
      </el-table-column>



      <el-table-column label="商家图片" align="center" prop="img" >
        <template slot-scope="scope">
          <img :src="$picbaserurl + scope.row.img" :width="50" :height="50">
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:merchant:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:merchant:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination small v-show="total > 0" :total="total" :current-page.sync="queryParams.pageNum"
      :page-size.sync="queryParams.pageSize" background layout="prev, pager, next" @current-change="getList" />

    <!-- 添加或修改merchant对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商家分类">
          <el-select v-model="form.catid" placeholder="请选择商家分类" >
         <el-option v-for="index in catageory" :value="index.value" :label="index.name" :key="index.value"></el-option>
      
        </el-select>
        </el-form-item>
        
        
        <el-form-item label="商家名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入商家名字" />
        </el-form-item>
        <el-form-item label="商家评分" prop="score">
          <el-input v-model="form.score" placeholder="请输入商家评分" />
        </el-form-item>
        <el-form-item label="商家手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入商家手机号" />
        </el-form-item>
        <el-form-item label="商家地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入商家地址" />
        </el-form-item>
        <el-form-item label="起送价格" prop="deliveryprice">
          <el-input v-model="form.deliveryprice" placeholder="请输入起送价格" />
        </el-form-item>
        <el-form-item label="发布状态" prop="publishstate">
          <el-input v-model="form.publishstate" placeholder="请输入发布状态" />
        </el-form-item>
        <el-form-item label="商家图片" prop="img">

          <el-upload class="avatar-uploader" :action="$picbaserurl + '/upload'" :show-file-list="false"
            :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="form.img" :src="$picbaserurl + form.img" class="avatar" style="width: 60px; height: 60px;" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMerchant, getMerchant, delMerchant, addMerchant, updateMerchant } from "@/api/merchant";
import { listCategory } from "@/api/category";
export default {
  name: "Merchant",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // merchant表格数据
      merchantList: [],
      //分类的数据
      catageory:[],
      //是否显示
      isshow:1,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        catid: null,
        name: null,
        score: null,
        phone: null,
        address: null,
        publishstate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询merchant列表 */
    getList() {
      this.loading = true;
      listMerchant(this.queryParams).then(response => {
        this.merchantList = response.data.rows;
        this.total = response.data.totalsize;
        this.loading = false;
      });
      listCategory(this.isshow).then(res =>{
          res.data.rows.forEach(element => {
            this.catageory.push({name:element.description,value:element.catid})
          })
            
      })
      console.log(this.catageory);
    },
    


    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        merchantid: null,
        catid: null,
        name: null,
        score: null,
        phone: null,
        address: null,
        publishstate: null,
        img: null
      };
      this.$nextTick(() => {
        this.resetForm("form");
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$nextTick(() => {
        this.resetForm("queryForm").resetFields();
      })
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.merchantid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加merchant";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const merchantid = row.merchantid || this.ids
      getMerchant(merchantid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改merchant";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.merchantid != null) {
            updateMerchant(this.form).then(response => {
              this.$message({ type: 'success', message: '修改成功' })
              this.open = false;
              this.getList();
            });
          } else {
            addMerchant(this.form).then(response => {
              this.$message({ type: 'success', message: '新增成功' })
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const merchantids = row.merchantid || this.ids;
      this.$confirm('是否确认删除banners编号为"' + merchantids + '"的数据项？','提示',{confirmButtonText:'确定',cancelButtonText:'取消',type:'warning'}).then(function(){
        return delMerchant(merchantids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/merchant/export', {
        ...this.queryParams
      }, `merchant_${new Date().getTime()}.xlsx`)
    },
    handleAvatarSuccess(res, file) {
      this.form.img= res.data;
      console.log(this.form.img);
    },
    beforeAvatarUpload(file) {


    },
  }
};
</script>
